package com.jyy.myserviceapp;

import android.app.ActivityOptions;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaRouter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Display;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ShellUtils;

public class MyServiceMainActivity extends AppCompatActivity {

    private final Handler mainHandler = new Handler(Looper.getMainLooper());
    private Intent mServiceIntent;
    private StartAppReceiver mStartAppReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 无界面显示
        setContentView(R.layout.activity_main);

        Log.d(LogTag.TAG, "服务端: MyServiceMainActivity onCreate");
//        mServiceIntent = new Intent(this, MyService.class);
//        startService(mServiceIntent);


        mStartAppReceiver = new StartAppReceiver();
        IntentFilter intentFilter = new IntentFilter("com.jyy.myserviceapp.MY_CUSTOM_ACTION");
        registerReceiver(mStartAppReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.d(LogTag.TAG, "服务端: MyServiceMainActivity onDestroy");
//        stopService(mServiceIntent);

        unregisterReceiver(mStartAppReceiver);
    }

    private final class StartAppReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("com.jyy.myserviceapp.MY_CUSTOM_ACTION".equals(action)) {
                String packageName = intent.getStringExtra("packageName");
                Log.d(LogTag.TAG, "服务端: 收到启动app请求: " + packageName);
                // TODO 方式1，只在当前屏幕显示
//                final Intent intent1 = new Intent();
//                intent1.setComponent(new ComponentName("com.jyy.signapp", "com.jyy.signapp.MainActivity"));
//                startActivity(intent1);
                //TODO 方式2 通过 MediaRouter 查找屏幕
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    showSecondByActivity(MyServiceMainActivity.this);
                }

                // TODO 方式3 使用 am 命令
//                final ShellUtils.CommandResult commandResult = ShellUtils.execCmd("adb shell am start --display 1 com.jyy.signapp/com.jyy.signapp.MainActivity", true);
//                Log.d(LogTag.TAG, "服务端: 启动app结果: " + commandResult.result
//                        + ",successMsg: " + commandResult.successMsg + ",errorMsg: " + commandResult.errorMsg);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void showSecondByActivity(Context context){
        ActivityOptions options = ActivityOptions.makeBasic();
        MediaRouter mediaRouter = (MediaRouter)
                context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        MediaRouter.RouteInfo route =
                mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
        if (route != null) {
            Display presentationDisplay = route.getPresentationDisplay();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(presentationDisplay.getDisplayId());
            }
            final Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.jyy.signapp", "com.jyy.signapp.MainActivity"));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent, options.toBundle());
        }
    }
}